﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DTO;
using DAO;

namespace QLDaiLy
{
    public partial class PhanQuyen : XtraUserControl
    {
        private string MaNhanVien;

        public PhanQuyen()
        {
            InitializeComponent();
            LoadDanhSachNhanVien();
            LoadDanhSachChucVu();
           
        }

        public PhanQuyen(string MaNhanVien)
        {
            // TODO: Complete member initialization
            InitializeComponent();
            LoadDanhSachNhanVien();
            LoadDanhSachChucVu();
            this.MaNhanVien = MaNhanVien;
            lookupTenNhanVien.EditValue = MaNhanVien;
        }
        private void LoadDanhSachNhanVien()
        {
            List<NhanVienDTO> list = new List<NhanVienDTO>();
            list = NhanVienDAO.LoadDanhSachNhanVien();
            lookupTenNhanVien.Properties.DataSource = list;
            lookupTenNhanVien.Properties.ValueMember = "MaNhanVien";
            lookupTenNhanVien.Properties.DisplayMember = "TenNhanVien";
            DevExpress.XtraEditors.Controls.LookUpColumnInfo colTenLoaiDaiLy;
            colTenLoaiDaiLy = new DevExpress.XtraEditors.Controls.LookUpColumnInfo("TenNhanVien");
            colTenLoaiDaiLy.SortOrder = DevExpress.Data.ColumnSortOrder.Ascending;
            lookupTenNhanVien.Properties.Columns.Add(colTenLoaiDaiLy);
        }
        private void LoadDanhSachChucVu()
        {
            List<ChucVuDTO> list = new List<ChucVuDTO>();
            list = ChucVuDAO.LoadChucVu();
            lookupTenChucVu.Properties.DataSource = list;
            lookupTenChucVu.Properties.ValueMember = "MaChucVu";
            lookupTenChucVu.Properties.DisplayMember = "TenChucVu";
            DevExpress.XtraEditors.Controls.LookUpColumnInfo colTenLoaiDaiLy;
            colTenLoaiDaiLy = new DevExpress.XtraEditors.Controls.LookUpColumnInfo("TenChucVu");
            colTenLoaiDaiLy.SortOrder = DevExpress.Data.ColumnSortOrder.Ascending;
            lookupTenChucVu.Properties.Columns.Add(colTenLoaiDaiLy);
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void btnPhanQuyen_Click(object sender, EventArgs e)
        {
            if (lookupTenNhanVien.Text != "" && lookupTenChucVu.Text != "")
            {
                string maNhanVien = lookupTenNhanVien.EditValue.ToString();
                string maChucVu = lookupTenChucVu.EditValue.ToString();

                bool kq = NhanVienDAO.UpDateChucVu(maNhanVien, maChucVu);
                if (kq)
                {
                    MessageBox.Show("Phân quyền thành công");
                    lookupTenChucVu.EditValue = "Chọn.....";
                    lookupTenNhanVien.EditValue = "Chọn.....";
                }
                else
                {
                    MessageBox.Show("Phân quyền thất bại");
                    lookupTenChucVu.EditValue = "Chọn.....";
                    lookupTenNhanVien.EditValue = "Chọn.....";
                }
            }
            else
            {
                MessageBox.Show("Các Ô không dược để trống");
            }
        }
    }
}
